/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hd.assistant.web.home.module.screen;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.hd.assistant.biz.manager.ClickedRedirectManager;
import com.hd.assistant.dal.dataobject.User;
import com.hd.assistant.web.common.util.ClickedUtil;

/**
 * 外部重定向，用来示范redirect token的使用方法。
 *
 * @author Michael Zhou
 */
public class Redirect {
    private final static Logger log = LoggerFactory.getLogger(Redirect.class);

    @Autowired
    ClickedRedirectManager clickedRedirectManager;
    public void execute(@Param("cid") String cid, Navigator nav) {
        if (cid != null) {
          String url = generateUrl(cid);
          nav.redirectToLocation(url);
        } else {
            nav.forwardTo("homepage");
        }
    }

	private String generateUrl(String cid) {
		String type = cid.substring(0,2);
		String t = type.equals(ClickedUtil.PRE_PAIPAI)?User.Type_PAIPAI:(type.equals(ClickedUtil.PRE_TAOBAO)?User.Type_TAOBAO:null);
		clickedRedirectManager.autoIncrement(cid);
		String itemCode = ClickedUtil.getItemIdByclickedId(cid);
		if(t.equals(User.Type_PAIPAI)){
			return "http://auction1.paipai.com/"+itemCode;
		}
		if(t.equals(User.Type_TAOBAO)){
			return "http://item.taobao.com/item.htm?id="+itemCode;
		}
		return null;
	}
}
